home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / code / p_serlib.sit / Serial Library Source Code / serial.write.dll.c < prev    next >
C/C++ Source or Header  |  1989-07-27  |  5KB  |  177 lines

  1. /***********************************************************************/
  2. /*    
  3. /*    serial.write.dll.c
  4. /*    by Atul Butte
  5. /*    Copyright ⌐ 1989 by Microsoft Corporation
  6. /*    All Rights Reserved
  7. /*
  8. /*    version 1.0
  9. /*    
  10. /*    
  11. /*    This CALL/REGISTER will send a string through a serial port.
  12. /*    
  13. /*    Excel usage:
  14. /*    
  15. /*    = Register( "serial library", "serial.write", "IHCD" )
  16. /*    = Call( ref, portNumber, writeConfigStr, string )
  17. /*    
  18. /*    where
  19. /*        portNumber        = number of port (1 = modem, 2 = printer)
  20. /*        writeConfigStr    = configuration of communications protocol, etc
  21. /*        string            = string to send
  22. /*    
  23. /***********************************************************************/
  24.  
  25. /***********************************************************************/
  26. /*
  27. /*    D E F I N E S
  28. /*
  29. /***********************************************************************/
  30.  
  31. #define ROUTINE_NAME        "serial.write"
  32. #define hNIL 0L
  33. #define pNIL 0L
  34.  
  35. /***********************************************************************/
  36. /*
  37. /*    I N C L U D E S
  38. /*
  39. /***********************************************************************/
  40.  
  41. #include "serial.h"
  42. #include "error.h"
  43. #include "get_port.h"
  44. #include "interpret.h"
  45. #include "absorb_echo.h"
  46. #include "get_write_flags.h"
  47.  
  48. /***********************************************************************/
  49. /*
  50. /*    main
  51. /*
  52. /***********************************************************************/
  53.  
  54. pascal short main( port, pszConfig, pst )
  55.  
  56.     unsigned short            port;                    /* serial port to use */
  57.     register char            *pszConfig;                /* communications configuration string */
  58.     register char            *pst;                    /* holds the string to send */
  59.     
  60. {
  61.     register OSErr            err;                    /* result code from Toolbox routines */
  62.     ParamBlockRec            param;                    /* parameter block for read/write */
  63.     
  64.     Boolean                    fAddLF = false;            /* flag for adding linefeeds */
  65.     Boolean                    fIgnore = false;        /* flag for ignoring escape chars */
  66.     Boolean                    fStrip8Bit = false;        /* flag for stripping high bit */
  67.     Boolean                    fAbsorbEcho = false;    /* flag for waiting for echo from host */
  68.     
  69.     short                    refIn;                    /* reference number for input port */
  70.     short                    refOut;                    /* reference number for output port */
  71.     register long            ich = 0;                /* index in characters to send (# already sent) */
  72.     long                    cch;                    /* count of total characters to send */
  73.     long                    cchBuff = 0;            /* number of characters waiting in read buffer */
  74.     char                    ch;                        /* character to write */
  75.     
  76.     RememberA0();
  77.     SetUpA4();
  78.     if( pszConfig == pNIL ) {
  79.         display_error( "The second parameter must be a configuration string." );
  80.         RestoreA4( );
  81.         return( errParam );
  82.     }
  83.     
  84.     err = get_port( port, &refIn, &refOut );
  85.     if( err != noErr ) {
  86.         display_error( "Illegal port number." );
  87.         RestoreA4( );
  88.         return( err );
  89.     }
  90.     
  91.     get_write_flags( pszConfig, &fAddLF, &fIgnore, &fStrip8Bit, &fAbsorbEcho );
  92.     
  93.     if( !fIgnore ) {
  94.         err = interpret( pst );
  95.         if( err != noErr ) {
  96.             RestoreA4( );
  97.             return( errStrFormat );
  98.         }
  99.     }
  100.     
  101.     if( fAbsorbEcho ) {            /* flush input buffer */
  102.         do {
  103.             err = SerGetBuf( refIn, &cchBuff );
  104.             if( err != noErr ) {
  105.                 display_error( "Error trying to count buffer." );
  106.                 break;
  107.             }
  108.             if( cchBuff != 0 ) {
  109.                 param.ioParam.ioReqCount = 1;
  110.                 param.ioParam.ioBuffer = &ch;
  111.                 param.ioParam.ioRefNum = refIn;
  112.                 err = PBRead( ¶m, false );
  113.                 if( err != noErr ) {
  114.                     display_error( "Error trying to flush the input buffer." );
  115.                     return( errSerialRead );
  116.                 }
  117.             }
  118.         } while( cchBuff != 0 );
  119.     }
  120.     
  121.     ich = 0;
  122.     cch = *pst;
  123.     pst++;
  124.     
  125.     while( ich < cch ) {
  126.     
  127.         ch = *pst;
  128.         if( fStrip8Bit ) {
  129.             ch &= 0x7f;
  130.         }
  131.         param.ioParam.ioReqCount = 1;
  132.         param.ioParam.ioBuffer = &ch;
  133.         param.ioParam.ioRefNum = refOut;
  134.         err = PBWrite( ¶m, false );
  135.         if( err != noErr ) {
  136.             display_error( "Error writing to serial port." );
  137.             RestoreA4();
  138.             return( errSerialWrite );
  139.         }
  140.         if( fAbsorbEcho ) {
  141.             if( absorb_echo( refIn, ch ) != noErr ) {
  142.                 display_error( "Error reading echo from serial port." );
  143.                 RestoreA4();
  144.                 return( errSerialWrite );
  145.             }
  146.         }
  147.         if( (ch == kchReturn) && (fAddLF) ) {
  148.             ch = kchLinefeed;
  149.             param.ioParam.ioReqCount = 1;
  150.             param.ioParam.ioBuffer = &ch;
  151.             param.ioParam.ioRefNum = refOut;
  152.             err = PBWrite( ¶m, false );
  153.             if( err != noErr ) {
  154.                 display_error( "Error writing linefeed to serial port." );
  155.                 RestoreA4();
  156.                 return( errSerialWrite );
  157.             }
  158.             if( fAbsorbEcho ) {
  159.                 if( absorb_echo( refIn, ch ) != noErr ) {
  160.                     display_error( "Error reading echo from serial port." );
  161.                     RestoreA4();
  162.                     return( errSerialRead );
  163.                 }
  164.             }
  165.         }
  166.         pst++;
  167.         ich++;
  168.     }
  169.     RestoreA4();
  170.     return( noErr );
  171. }
  172.  
  173. #include "get_write_flags.c"
  174. #include "get_port.c"
  175. #include "interpret.c"
  176. #include "absorb_echo.c"
  177.